//
//  RootViewController.m
//  自适应瀑布流
//
//  Created by dllo on 16/3/10.
//  Copyright © 2016年 dllo. All rights reserved.
//

#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height

#import "RootViewController.h"

#import "CollectionViewCell.h"

#import "BHFlowLayout.h"

#import "AFNetworking.h"
#import "UIImageView+WebCache.h"

#import "MediaModel.h"

@interface RootViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,BHFlowLayoutDelegate>
// 显示
@property(nonatomic, strong)UICollectionView *collectionView;
// 保存数据
@property(nonatomic, strong)NSMutableArray *dataArr;

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 创建视图
    [self createView];
    
    // 读取数据
    [self createData];
    
}
#pragma mark 创建视图
- (void)createView{
    self.view.backgroundColor = [UIColor colorWithRed:246 / 255.0 green:246 / 255.0 blue:246 / 255.0 alpha:1];
    
    
    
    BHFlowLayout *flowLayout = [[BHFlowLayout alloc] init];
    // 边框
    flowLayout.edgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
    // 列数
    flowLayout.columnCounts = 2;
    // 列间距
    flowLayout.columnSpace = 5;
    // 行间距
    flowLayout.rowSpace = 5;
    // 代理人
    flowLayout.delegate = self;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight - 64 - 5) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor colorWithRed:246 / 255.0 green:246 / 255.0 blue:246 / 255.0 alpha:1];
    [self.view addSubview:self.collectionView];
    
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    
    [self.collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"collectionView"];
}

#pragma mark CollectionView的协议方法
// Item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.dataArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionView" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor colorWithRed:227 / 255.0 green:227 /255.0 blue:227 / 255.0 alpha:1];
    
    MediaModel *mediaModel = self.dataArr[indexPath.row];
    
    [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:mediaModel.recommend_cover_pic]];
    
    return cell;
}
// 点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSLog(@"点击%ld",indexPath.row);
}

#pragma mark 计算Item的高
- (CGFloat)bhFlowLayout:(BHFlowLayout *)flowLayout heightForRowAtIndexPath:(NSIndexPath *)indexPath width:(CGFloat)width{
    // 高 = 原高 * ItemWidth / 原宽
    MediaModel *mVideosModel = self.dataArr[indexPath.row];
    
    CGFloat height = mVideosModel.mVideosHeight * width / mVideosModel.mVideosWidth;
    return height;
}

#pragma mark 读取数据
- (void)createData{
    
    NSString *strURL = [NSString stringWithFormat:@"https://newapi.meipai.com/hot/feed_timeline.json?page=1&count=50&language=zh-Hans&client_id=1089857302&device_id=358511020042960&version=4510&channel=baidu&model=4G&locale=1&sig=4e1894f0c7549c43f77f834022e12023&sigVersion=1.0"];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//    [manager.requestSerializer setValue:@"5387fa43123013c700bf99ed544caad8" forHTTPHeaderField:@"token"];
    [manager GET:strURL parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        self.dataArr = [MediaModel baseModelByArr:responseObject];
        
        [self.collectionView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
